package com.oncliedisk.administrator.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.oncliedisk.administrator.interfaces.onDownFileListener;
import com.oncliedisk.administrator.model.FileTaskModel;
import com.oncliedisk.administrator.utils.DownFile;
import com.vdisk.android.VDiskAuthSession;
import com.vdisk.net.VDiskAPI;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/20.
 */
public class TransmissionService extends Service {

    private IBinder binder;
    private List<DownFile> list = new ArrayList<DownFile>();
    public static boolean isDowning; //正在下载
    private boolean isTopDown; //是否是第一次要下载文件
    public static final int THEAD_CLOSE = 0;
    public static int THREAD_EXCEL = 1;

    public IBinder onBind(Intent intent) {
        binder = new MyBinder();
        return binder;
    }

    public class MyBinder extends Binder {

        /**
         * 获取Service对象
         *
         * @return
         */
        public TransmissionService getService() {
            return TransmissionService.this;
        }
    }

    /**
     * 下载文件
     */
    public void downFile() {
        //如果是第一次把要下载的文件添加到下载列表，就从第一个开始下载。如果是第二次要下载，那就不做操作了，等待其他文件下载成功后，按顺序下载下去
        if (isTopDown == true) {
            //如果是第一次下载，
            isTopDown = false;
            DownFile downFile = list.get(0);
            downFile.execute();
        }
    }

    /**
     * 是要关闭线程还是继续线程
     */
    public void downAction(FileTaskModel molder, int type, Context context, VDiskAPI<VDiskAuthSession> mApi, onDownFileListener onDownFileListener, DownFile downFile) {
        DownFile newTask = null;
        DownFile oldTask = null;
        //根据对象，开启对象对应的线程进行下载
        for (DownFile file : list) {
            if (file != null) {
                if (file.model.getId() == molder.getId()) {
                    if (type == THEAD_CLOSE) {
                        file.closeThead();
                        oldTask = file;
                    } else if (type == THREAD_EXCEL) {
                        //如果不关闭，要进行继续,则重新开启一个线程
                        oldTask = null;
                    }
                }
            }
        }
        if (oldTask != null) {
            //如果要
            list.remove(oldTask);
        } else {
            list.add(downFile);
        }
    }


    /**
     * 下载成功后，默认下载第一个文件
     */
    public void downFileByFinish(FileTaskModel model, Context context, VDiskAPI<VDiskAuthSession> mApi, onDownFileListener onDownFileListener) {
        DownFile files = null;
        for (DownFile file : list) {
            if (file != null) {
                if (file.model.getId() == model.getId()) {
                    files = file;
                }
            }
        }
        list.remove(files);
        //如果没有线程正在下载
        if (list.size() > 0) {
            DownFile downFile = list.get(0);
            if(downFile != null){
                FileTaskModel model1 = downFile.model;
                list.remove(downFile);
                DownFile downFile1 = new DownFile(context, model1, mApi, onDownFileListener);
                list.add(0, downFile1);
                downFile1.execute();
            }
        }
    }


    /**
     * 添加要下载的文件对象
     *
     * @param model
     */
    public void addFileModel(Context context, FileTaskModel model, VDiskAPI<VDiskAuthSession> mApi, onDownFileListener onDownFileListener) {
        DownFile file = new DownFile(context, model, mApi, onDownFileListener);
        if (!list.contains(file)) {
            if (list.size() == 0) {
                isTopDown = true;
            }
            list.add(file);
        }
    }

    /**
     * 停止线程
     */
    public void stopDownFile(FileTaskModel model) {
        for (DownFile model1 : list) {
            if (model1.model.getId() == model.getId()) {
                model1.closeThead();
            }
        }
    }

    public void setIsCancel() {
//        file.setIsCancel(false);
    }

}
